package com.clementheliou.cheliou.util.math.average;

import static org.apache.commons.collections.CollectionUtils.isEmpty;

import java.util.List;

/**
 * Utilities about the average of a set of values.
 * 
 * @author Clément HELIOU (clement.heliou@gmail.com)
 * @since 1.0
 */
public abstract class AverageUtils {

	/**
	 * <p>
	 * Get the average mark of the given mark's list. This one must not be null
	 * or empty.
	 * </p>
	 * <p>
	 * <b><u>NOTE:</u></b><br/>
	 * You should not use this method if the sum of the source list is
	 * potentially greater than {@link Long#MAX_VALUE}.
	 * </p>
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @param marks the source list.
	 * @return the resulting average mark.
	 * @since 1.0
	 * @throws IllegalArgumentException if the given list is null or empty.
	 */
	public static AverageMark getAverageMark(final List<Integer> marks) {

		if (isEmpty(marks)) {
			throw new IllegalArgumentException("The marks parameter must not be null at this time.");
		}

		long sum = 0;
		for (Integer mark : marks) {
			sum += mark;
		}

		return new AverageMark(((double) sum) / marks.size(), marks.size());
	}
}
